package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.key.RedisKeys;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Reference
    private IUserInfoService userInfoService;

    /**
     * 检查注册的手机号码是否存在
     *
     * @param phone
     * @return
     */
    @RequestMapping("checkPhone")
    @ResponseBody
    public Object checkPhone(String phone) {

        return !userInfoService.checkPhone(phone);
    }
    /**
     * 检查更改的邮箱是否存在
     *
     * @param email
     * @return
     */
    @RequestMapping("checkEmail")
    @ResponseBody
    public Object checkEmail(String email, @UserParam UserInfo userInfo) {
        Long id = userInfo.getId();
        return !userInfoService.checkEmail(email,id);
    }
    /**
     * 检查更改的手机是否存在
     *
     * @param phone
     * @return
     */
    @RequestMapping("checkNewPhone")
    @ResponseBody
    public Object checkNewPhone(String phone, @UserParam UserInfo userInfo) {
        Long id = userInfo.getId();
        return !userInfoService.checkNewPhone(phone,id);
    }

    /**
     * 发送短信验证码
     *
     * @param phone
     * @return
     */
    @RequestMapping("sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone);
        return new AjaxResult();
    }

    /**
     * 发送邮件验证码
     * @param email
     * @return
     */
    @RequestMapping("sendVerifyCodeToEmail")
    @ResponseBody
    public Object sendVerifyCodeToEmail(String email) {
        userInfoService.sendVerifyCodeToEmail(email);
        return new AjaxResult();
    }
    /**
     * 发送新邮件的验证码
     * @param temporaryEmail
     * @return
     */
    @RequireLogin
    @RequestMapping("sendVerifyCodeToTemporaryEmail")
    @ResponseBody
    public Object sendVerifyCodeToTemporaryEmail(String temporaryEmail) {
        userInfoService.sendVerifyCodeToTemporaryEmail(temporaryEmail);
        return new AjaxResult();
    }

    /**
     * 发送新手机的验证码
     * @param temporaryPhone
     * @return
     */
    @RequireLogin
    @RequestMapping("sendVerifyCodeToTemporaryPhone")
    @ResponseBody
    public Object sendVerifyCodeToTemporaryPhone(String temporaryPhone) {
        userInfoService.sendVerifyCode(temporaryPhone);
        return new AjaxResult();
    }


    /**
     * 用户注册
     *
     * @param phone      页面提交的手机号码
     * @param nickname   页面提交的昵称
     * @param password   页面提交的密码
     * @param rpassword  页面提交的确认密码
     * @param verifyCode 页面提交的验证码
     * @return
     */
    @RequestMapping("userRegist")
    @ResponseBody
    public Object userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
        return new AjaxResult();
    }

    /**
     * 用户登录
     * @param username 账号名(手机号)
     * @param password 密码
     * @return
     */
    @RequestMapping("userLogin")
    @ResponseBody
    public Object userLogin(String username, String password, HttpServletResponse resp) {
        //获得令牌
        String token = userInfoService.userLogin(username,password);
        //创建cookie设置值并响应给浏览器
        CookieUtil.addCookie("token",token,resp,RedisKeys.USER_INFO_LOGIN.getTime().intValue());

        return new AjaxResult();
    }


    @RequestMapping("userUpdatePassword")
    @ResponseBody
    public Object userUpdatePassword(String phone, String password, String rpassword, String verifyCode) {
        userInfoService.userUpdatePassword(phone, password, rpassword, verifyCode);
        return new AjaxResult();
    }

    @RequestMapping("userUpdatePasswordByEmail")
    @ResponseBody
    public Object userUpdatePasswordByEmail(String email, String password, String rpassword, String verifyCode) {
        userInfoService.userUpdatePasswordByEmail(email, password, rpassword, verifyCode);
        return new AjaxResult();
    }

    @RequireLogin
    @RequestMapping("userUpdateEmailByTemporaryEmail")
    @ResponseBody
    public Object userUpdateEmailByTemporaryEmail(String temporaryEmail,String verifyCode) {
        userInfoService.userUpdateEmailByTemporaryEmail(temporaryEmail,verifyCode);
        return new AjaxResult();
    }

    @RequireLogin
    @RequestMapping("userUpdatePhoneByTemporaryPhone")
    @ResponseBody
    public Object userUpdatePhoneByTemporaryPhone(String temporaryPhone,String verifyCode) {
        userInfoService.userUpdatePhoneByTemporaryPhone(temporaryPhone,verifyCode);
        return new AjaxResult();
    }

}
